﻿#include "dataThread.h"

// 上一次的数据路径
//{
//    "sim/flightmodel/position/theta",          // pitch
//        "sim/flightmodel/position/phi",            // roll
//        "sim/cockpit2/gauges/indicators/altitude_ft_pilot",  // heightAM
//        "sim/flightmodel/position/indicated_airspeed",       // airspeed
//        "sim/flightmodel/position/vh_ind_fpm",         // verticalSpeed
//        "sim/cockpit2/gauges/indicators/turn_rate_heading_deg_pilot",  // turn_rate
//        "sim/cockpit2/gauges/indicators/slip_deg", // turn_rate2
//        "sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot", // heading
//        "sim/cockpit/radios/nav1_hdef_dot",        // vor1hdef
//        "sim/cockpit/radios/nav2_hdef_dot",        // vor2hdef
//        "sim/cockpit/radios/nav1_fromto",          // vor1dr
//        "sim/cockpit/radios/nav2_fromto",          // vor2dr
//        "sim/flightmodel/engine/ENGN_oil_temp_c",  // Oilc
//        "sim/flightmodel/engine/ENGN_oil_press_psi", // Oilp
//        "sim/flightmodel/engine/ENGN_bat_amp",     // I
//        "sim/cockpit/misc/vacuum",                 // pressure
//        "sim/flightmodel/weight/m_fuel1",          // left
//        "sim/flightmodel/weight/m_fuel2",          // right
//        "sim/flightmodel/engine/ENGN_FF_",         // engine
//        "sim/cockpit/radios/nav1_vdef_dot",         // vor1vdf
//        "sim/flightmodel/engine/ENGN_tacrad"        // engineRMB
//}

//定义数据共享结构体
DashboardData sharedData;

// 数据获取线程函数
void xplaneDataThread() {

    std::cout << "线程已启动" << std::endl;
    const char* IP = MYIP;      // IP Address of computer running X-Plane
    XPCSocket sock = openUDP(IP);

    //{
    //    getDREF(sock, "sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot", &angle, &sizeas);//航向
    //    getDREF(sock, "sim/cockpit/radios/nav1_dme_dist_m", &VOR1HL, &sizeas);//vor1海里
    //    getDREF(sock, "sim/cockpit/radios/nav2_dme_dist_m", &VOR2HL, &sizeas);//vor2海里
    //    getDREF(sock, "sim/cockpit2/radios/indicators/nav1_bearing_deg_mag", &VOR1D, &sizeas);//vor1指向
    //    getDREF(sock, "sim/cockpit2/radios/indicators/nav2_bearing_deg_mag", &VOR2D, &sizeas);//vor2指向
    //    getDREF(sock, "sim/flightmodel/position/true_airspeed", &GS, &sizeas);//地速
    //    getDREF(sock, "sim/cockpit2/gauges/indicators/altitude_ft_stby", &hight, &sizeas);//高度
    //    getDREF(sock, "sim/cockpit2/gauges/indicators/ground_track_mag_pilot", &Rdirection, &sizeas);//绿色的线指向
    //    getDREF(sock, "sim/flightmodel/position/true_theta", &pitch, &sizeas);//俯仰角
    //}

    // 定义所有需要获取的 DREF 路径（按顺序）
    const char* drefs[] = {
        "sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot",
        "sim/cockpit/radios/nav1_dme_dist_m",
        "sim/cockpit/radios/nav2_dme_dist_m",
        "sim/cockpit2/radios/indicators/nav1_bearing_deg_mag",
        "sim/cockpit2/radios/indicators/nav2_bearing_deg_mag",
        "1-sim/fms/gps1groundspeed",
        "sim/cockpit2/gauges/indicators/altitude_ft_stby",
        "sim/cockpit2/gauges/indicators/ground_track_mag_pilot",
        "sim/flightmodel/position/true_theta",
        "1-sim/fms/irs1windSpd",
        "AirbusFBW/WindDirCapt",
        "AirbusFBW/NDmodeCapt",
        "AirbusFBW/TASCapt",
        "1-sim/xxx/vor1_temp",
        "1-sim/xxx/vor2_temp",
        "airbus_qpac/pfdoutputs/captain/roll_angle",   //roll
        "sim/cockpit2/gauges/indicators/airspeed_kts_copilot",//speed
        "sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot",//heading
        "airbus_qpac/pfdoutputs/captain/vertical_speed",//Chuispeed

        //EWD
        "AirbusFBW/WriteFOB",          // FOB
		"sim/flightmodel/weight/m_total",            // GW
		"sim/cockpit2/temperature/outside_air_LE_temp_deg",  // TAT
		"AirbusFBW/CGLocationPercent",       // GW_CG
		"sim/cockpit2/temperature/outside_air_temp_deg",         // SAT
		"1-sim/ind/eng/curTHR/L", // THR
		"sim/cockpit/pressure/cabin_pressure_differential_psi", // PSI
		"sim/cockpit2/pressurization/indicators/cabin_altitude_ft",        // FT
		"AirbusFBW/THRRatingN1",        // N1

        //ENG
        "sim/cockpit2/engine/indicators/N2_percent",        // N3

        "1-sim/lamp/320",        // WHEEL变化
        "1-sim/lamp/318",        // ENG变化
        "1-sim/lamp/358",        // DEFRD变化

        "AirbusFBW/OSpeedWarning",        // 速度报警

        // 襟翼
        "sim/cockpit2/controls/flap_ratio",
        "sim/cockpit/switches/gear_handle_status",
    };

    int size = sizeof(drefs) / sizeof(drefs[0]);

    // 动态分配values数组
    float** values = (float**)malloc(size * sizeof(float*));

    // 将count改为int类型
    int count = size;

    // 动态分配sizes数组
    int* sizes = (int*)malloc(count * sizeof(int));

    for (int i = 0; i < count; i++)
    {
        sizes[i] = 1;
        values[i] = (float*)malloc(1 * sizeof(float));
    }

    for (; is_run();) {
        {
            std::lock_guard<std::mutex> lock(sharedData.mtx);

            // 调用 getDREFs 函数
            int result = getDREFs(sock, drefs, values, count, sizes);

            // 错误处理
            if (result < 0) {
                sock = openUDP(IP);  // 重新连接
                std::cout << "Wrong" << std::endl;
            }
            else {
                // 解析数据（需严格对应 drefs 顺序）
                sharedData.headingAngle = values[0][0];
                sharedData.VOR1HL = values[1][0];
                sharedData.VOR2HL = values[2][0];
                sharedData.VOR1D = values[3][0];
                sharedData.VOR2D = values[4][0];
                sharedData.GS = values[5][0];
                sharedData.height = values[6][0];
                sharedData.Rdirection = values[7][0];
                sharedData.pitch = values[8][0];
                sharedData.WinV = values[9][0];
                sharedData.WinD = values[10][0];
                sharedData.choice1 = values[11][0];
                sharedData.TAS = values[12][0];
                sharedData.VOR1 = values[13][0];
                sharedData.VOR2 = values[14][0];
                sharedData.roll = values[15][0];
                sharedData.speed = values[16][0];
                sharedData.heading = values[17][0];
                sharedData.Chuispeed = values[18][0];

                //EWD
                sharedData.FOB = values[19][0];
                sharedData.GW = values[20][0];
                sharedData.TAT = values[21][0];
                sharedData.GW_CG = values[22][0];
                sharedData.SAT = values[23][0];
                sharedData.THR = values[24][0];
                sharedData.PSI = values[25][0];
                sharedData.FT = values[26][0];
                sharedData.N1 = values[27][0];
                
                //ENG
                sharedData.N3 = values[28][0];

                sharedData.Wheel = values[29][0];
                sharedData.Eng = values[30][0];
                sharedData.Defrd = values[31][0];

                sharedData.SpeedWD = values[32][0];

                //襟翼
                sharedData.landing = values[33][0];
                //起落架
                sharedData.gear1= values[34][0];
            }
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
        {
            // 输出飞行数据
            std::cout << "================= 飞行数据 =================" << std::endl;
            std::cout << "航向角:         " << sharedData.headingAngle << " 度" << std::endl;
            std::cout << "VOR1水平方位角: " << sharedData.VOR1HL * 180.0f / M_PI << " 度" << std::endl;
            std::cout << "VOR2水平方位角: " << sharedData.VOR2HL << " 度" << std::endl;
            std::cout << "VOR1距离:       " << sharedData.VOR1D << " 海里" << std::endl;
            std::cout << "VOR2距离:       " << sharedData.VOR2D << " 海里" << std::endl;
            std::cout << "地速:           " << sharedData.GS << " 节" << std::endl;
            std::cout << "高度:           " << sharedData.height << " 英尺" << std::endl;
            std::cout << "跑道方向:       " << sharedData.Rdirection << " 度" << std::endl;
            std::cout << "俯仰角:         " << sharedData.pitch << " 度" << std::endl;
            std::cout << "===========================================" << std::endl;
        }
    }

    //// 使用完毕后，别忘了释放内存
    for (int i = 0; i < count; i++) {
        free(values[i]);
    }
    free(values);
    free(sizes);
}